<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class History_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	/**
	 * 회원의 운세 이용 내역
	 */
	function select_service_history_list($user_id, $page_no, $page_size) {
		log_message('debug',"#### History_model::select_service_history_list($user_id, $page_no, $page_size)");
		
		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       S.idx ";
		$sql .= PHP_EOL."     , S.user_id ";
		$sql .= PHP_EOL."     , (SELECT service_name FROM tb_service WHERE service_id = S.service_id) service_name ";
		$sql .= PHP_EOL."     , S.cash ";
		$sql .= PHP_EOL."     , S.point ";
		$sql .= PHP_EOL."     , (SELECT service_group_name FROM tb_service_group WHERE service_group_id = S.coupon_id) coupon_name ";
		$sql .= PHP_EOL."     , S.ticket_idx ";
		$sql .= PHP_EOL."     , S.param_values ";
		$sql .= PHP_EOL."     , S.secret_yn ";
		$sql .= PHP_EOL."     , S.create_dt ";
		$sql .= PHP_EOL." FROM  tb_service_used_history S ";
		$sql .= PHP_EOL." WHERE 1=1 ";
		$sql .= PHP_EOL." AND   S.user_id = ? ";
		$sql .= PHP_EOL." ORDER BY S.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 총 내역 수
	 */
	function get_service_history_total_count($user_id) {
		log_message('debug',"#### History_model::get_service_history_total_count($user_id)");
		
		$this->db->where('user_id', $user_id);
		$this->db->from('tb_service_used_history');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}


	/**
	 * 회원의 포인트 적립/차감 내역
	 */
	function select_point_history_list($user_id, $page_no, $page_size) {
		log_message('debug',"#### History_model::select_point_history_list($user_id, $page_no, $page_size)");

		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT A.* ";
		$sql .= PHP_EOL." FROM ";
		$sql .= PHP_EOL." ( ";
		$sql .= PHP_EOL."     SELECT ";
		$sql .= PHP_EOL."           P.user_id ";
		$sql .= PHP_EOL."         , IF(P.point_type = 'C', '캐쉬충전', ' 보너스포인트') point_type ";
		$sql .= PHP_EOL."         , IF(P.cash=0,0,CONCAT('+',P.cash)) cash ";
		$sql .= PHP_EOL."         , IF(P.point=0,0,CONCAT('+',P.point)) point ";
		$sql .= PHP_EOL."         , (SELECT actv_caption FROM tb_activity_to_point WHERE actv_id = P.actv_id) subject ";
		$sql .= PHP_EOL."         , create_dt ";
		$sql .= PHP_EOL."     FROM  tb_point_charged_history P ";
		$sql .= PHP_EOL."  ";
		$sql .= PHP_EOL."     UNION ";
		$sql .= PHP_EOL."  ";
		$sql .= PHP_EOL."     SELECT ";
		$sql .= PHP_EOL."           S.user_id ";
		$sql .= PHP_EOL."         , '' point_type ";
		$sql .= PHP_EOL."         , IF(S.cash=0,0,CONCAT('-',S.cash)) cash ";
		$sql .= PHP_EOL."         , IF(S.point=0,0,CONCAT('-',S.point)) point ";
		$sql .= PHP_EOL."         , (SELECT service_name FROM tb_service WHERE service_id = S.service_id) subject ";
		$sql .= PHP_EOL."         , S.create_dt ";
		$sql .= PHP_EOL."     FROM  tb_service_used_history S ";
		$sql .= PHP_EOL." ) A ";
		$sql .= PHP_EOL." WHERE A.user_id = ? ";
		$sql .= PHP_EOL." AND   (A.point <> 0 OR A.cash <> 0) ";
		$sql .= PHP_EOL." ORDER BY A.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 총 글 수
	 */
	function get_point_history_total_count($user_id) {
		log_message('debug',"#### History_model::get_point_history_total_count($user_id)");

		$this->db->where('user_id', $user_id);
		$this->db->where('point  <>', 0);
		$this->db->from('tb_point_charged_history');
		$result1 = $this->db->count_all_results() or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
		
		$this->db->where('user_id', $user_id);
		$this->db->where('point <>', 0);
		$this->db->from('tb_service_used_history');
		$result2 = $this->db->count_all_results() or $this->logsql->log_sql('error');

		$this->logsql->log_sql();

		return $result1+$result2;
	}


	/**
	 * 회원의 결제 내역
	 */
	function select_payment_history_list($user_id, $page_no, $page_size) {
		log_message('debug',"#### History_model::select_payment_history_list($user_id, $page_no, $page_size)");

		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       P.user_id ";
		$sql .= PHP_EOL."     , P.pname ";
		$sql .= PHP_EOL."     , P.pltid ";
		$sql .= PHP_EOL."     , P.pgcode ";
		$sql .= PHP_EOL."     , P.amt ";
		$sql .= PHP_EOL."     , P.txdate ";
		$sql .= PHP_EOL."     , P.txtime ";
		$sql .= PHP_EOL."     , P.create_dt ";
		$sql .= PHP_EOL." FROM  tb_payment_history P ";
		$sql .= PHP_EOL." WHERE P.user_id = ? ";
		$sql .= PHP_EOL." ORDER BY P.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$this->total_count = $this->db->count_all_results();
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 총 글 수
	 */
	function get_payment_history_total_count($user_id) {
		log_message('debug',"#### History_model::get_payment_history_total_count($user_id)");
		
		$this->db->where('user_id', $user_id);
		$this->db->from('tb_payment_history');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
}

/* End of file history_model.php */
/* Location: ./application/models/member/history_model.php */